function [err] = binaryexport()
%
% write current dataset to a disk file with binary formatting. 
% creates output as follows:
% 1. makes a directory (if it does not already exist) with the current
% filename
% 2. creates a filename of the form: filename_startingrecord#.dat
% header information:
% 
% string: Original filename(256 characters)
% int 32: Starting record number in the original dataset
% int 32: Number of records in this block
% int 16: Block number
% float: sample rate
% int 16: # channels in dataset
% int 32: # points per channel in dataset
%

% find out about all the blocks, and read them in one-by-one - then do
% current_blockout to put the data out...

[drive, path, ext] = getdriveext;
lookpath=strcat(path, ext);
[filename, path] = uigetfile(lookpath, 'Export all data from Data File');
if(~ischar(filename))
    QueMessage(sprintf('Unable to access file? %s - %s', path, filename));
    result = 1;
    return;
end

ind = load([path filename], 'Index')
Index = ind.Index;
% first find all the data blocks...
for i = 1:length(Index)
    if(strmatch(Index(i).type, 'DATA'))
        fprintf(1, 'i: %d   %s: \n', i, Index(i).MatName);
        [DFILE, DATA, err] = datac2mat([path filename], [Index(i-1).record:Index(i).record-1]);
        current_blockout(DFILE, DATA);
    end;
end;



function current_blockout(DFILE, DATA)
if(isempty(DATA))
    return;
end;

pts = DFILE.nr_points;
ALLCH = cell(DFILE.nr_channel, pts);
p = (1:pts);
for j = 1:DFILE.nr_channel
    ALLCH{j} = DATA(:,p+(j-1)*pts);
end;


[p f e] = fileparts(DFILE.filename);
fn = [f sprintf('_%d', DFILE.record(1)) '.dat']
nr = length(DFILE.record);
mkdir('/users/pmanis/desktop', f);
h = fopen(sprintf('/users/pmanis/desktop/%s/%s', f, fn), 'wb');
fwrite(h, DFILE.filename, 'uchar');
ln = length(DFILE.filename);
fwrite(h, zeros(1,256-ln), 'uchar');
fwrite(h, DFILE.record(1), 'int32');
fwrite(h, nr, 'int32');
fwrite(h, DFILE.dblock, 'int32');
fwrite(h, DFILE.rate(1), 'float32');
fwrite(h, DFILE.channels(1), 'int32');
fwrite(h, DFILE.nr_points, 'int32');
for j = 1:DFILE.channels(1)
    x=ALLCH{j};
    for i = 1:nr   
        fwrite(h, x(i,:), 'float32');
    end;
end;

fclose(h);

